﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Common;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Data.Model;
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using ZLCMS.Business.Util;
using ZLCMS.Business.UploadFileInfo;

namespace ZLCMS.Web.SystemManage.Control.FileControl
{
    public partial class FileControl : System.Web.UI.UserControl
    {
        /// <summary>
        /// 设置或者获取附件列表的JSON数据
        /// </summary>
        /// <returns></returns>
        public string Value
        {
            set
            {
                ip_filelistjson.Value = value;
            }
            get
            {
                return ip_filelistjson.Value;
            }
        }

        /// <summary>
        /// 允许添加的附件类型，以|分隔。例如：jpg|png|jpeg|gif
        /// </summary>
        public string FileType { set; get; }

        private UploadConfig _config;

        /// <summary>
        /// 缩略图定义
        /// </summary>
        public UploadConfig Config { 
            
            get
            {
                // 无配置则采用默认配置
                if (_config == null)
                {
                    return new UploadConfig();
                }
                return _config;
            }
            
            set
            {
                _config = value;
            }
        }

        /// <summary>
        /// 是否显示‘现有添加’功能
        /// </summary>
        public bool IsShowHistory { get; set; }

        /// <summary>
        /// 是否允许删除
        /// </summary>
        public bool AllowDelete { get; set; }

        /// <summary>
        /// 控件是否可用
        /// </summary>
        public bool Enable { get; set; }

        private int _maxfiles = 5;
        /// <summary>
        /// 允许添加的附件最大数量，默认值为 5
        /// </summary>
        public int MaxFiles { set { _maxfiles = value; } get { return _maxfiles; } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ip_filelistjson.Value = Value;
                BindRepeator();
            }
        }

        private void BindRepeator()
        {
            if (!string.IsNullOrEmpty(Value))
            {
                try
                {
                    rp_filelist.DataSource = JsonConvert.DeserializeObject<List<SimpleFileInfo>>(Value);
                    rp_filelist.DataBind();
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }

        protected String GetFileSpanClass(String file)
        {
            String ext = file.Substring(file.LastIndexOf('.') + 1).ToLower();
            return "ext_" + ext;
        }

        protected String GetFileName(String file)
        {
            return file.Substring(file.LastIndexOf('/') + 1).ToLower();
        }

        protected String GetPreviewAnchor(String url)
        {
            String ext = url.Substring(url.LastIndexOf('.') + 1).ToLower() + ',';
            String imgs = "jpg,jpeg,gif,png,";

            if (imgs.IndexOf(ext) != -1)
            {
                return String.Format(@"<a href=""{0}"" class=""prettyPhoto"" title=""{1}"">预览</a>", url, url);
            }
            else
            {
                return @"<a style=""color: #C0C0C0"">预览</a>";
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }
    }
}